package ru.papergames.battleserver.network.clientpackets.webserver;

import org.apache.log4j.Logger;
import ru.papergames.battleserver.model.ConnectingFlashUser;
import ru.papergames.battleserver.model.UsersManager;
import ru.papergames.common.form.PcPlayerForm;
import ru.papergames.common.netutils.ModelReaderUtils;

/**
 * @author Alexander Troshanin
 */
public class AddNewPcPlayer extends WebServerClientPacket {

    private static final Logger log = Logger.getLogger(AddNewPcPlayer.class.getName());

    private PcPlayerForm player;
    private int secretKey;

    @Override
    protected void readImpl() {
        secretKey = readD();
        player = (PcPlayerForm) ModelReaderUtils.readPlayerFromBuffer(_buf);
    }

    @Override
    protected void runImpl() {
        log.debug("adding new player: " + player);
        UsersManager.getInstance().pushFlashUserInNewUsers(new ConnectingFlashUser(player, secretKey));
    }

    @Override
    public String getType() {
        return "[c] AddNewPcPlayer[" + ADD_NEW_PC_PLAYER_PACKET_ID + "]";
    }
}
